//
//  Mine_AddressbookViewController.m
//  fxxiaoke
//
//  Created by 鞠超 on 2016/11/11.
//  Copyright © 2016年 jc. All rights reserved.
//

#import "Mine_AddressbookViewController.h"

#import "Select_DirectlyleaderTableViewCell.h"
#import "Mine_AddressbookTableViewCell.h"

@interface Mine_AddressbookViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableView;

@property (nonatomic, strong) UITableView * tableView_Department;
@property (nonatomic, strong) UITableView * tableView_Conversation;
//tableview内部头视图
@property (nonatomic, strong) UIView * tableView_Head;

//头视图
@property (nonatomic, strong) UIView * view_Head;
@property (nonatomic, strong) UIView * view_Headline;

@property (nonatomic, strong) UIButton * btn_1;
@property (nonatomic, strong) UIButton * btn_2;
@property (nonatomic, strong) UIButton * btn_3;

//底部视图
@property (nonatomic, strong) UIView * view_Bottom;
@property (nonatomic, strong) UILabel * lbl_selected;
@property (nonatomic, strong) UIButton * btn_BottomDepartment;
@property (nonatomic, strong) UIButton * btn_define;

@end

@implementation Mine_AddressbookViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_navi];
    
    [self p_headView];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void )p_navi
{
    _btnLeft.hidden = YES;
    _lblLeft.hidden = YES;
    _btnRight.hidden = YES;
    _lblRight.hidden = YES;
    
    _lblTitle.text = @"通讯录";
    _topView.backgroundColor = navi_bar_bg_color;
    
    //左返回
    UIImageView * image_LeftRetrun = [[UIImageView alloc] initWithFrame:CGRectMake(11, 31.5, 25, 25)];
    image_LeftRetrun.userInteractionEnabled = YES;
    image_LeftRetrun.image = [UIImage imageNamed:@"Left-return.png"];
    [_topView addSubview:image_LeftRetrun];
    
    UILabel * lbl_return = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_LeftRetrun.frame) - 6, 35, 100, 15)];
    lbl_return.text = @"我";
    lbl_return.textColor = [UIColor whiteColor];
    lbl_return.font = [UIFont systemFontOfSize:17];
    [_topView addSubview:lbl_return];
    
    UIButton * btn_return = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_return.frame = CGRectMake(10, 22, 100, 40);
    [_topView addSubview:btn_return];
    [btn_return addTarget:self action:@selector(btn_returnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //...
    UIImageView * image_Setting = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 35, 32, 20, 20)];
    image_Setting.image = [UIImage imageNamed:@"Share_ Omit.png"];
    [_topView addSubview:image_Setting];
    
    UIButton * btn_Setting = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_Setting.frame = CGRectMake(SCREEN_WIDTH - 10 - 35, 24.5, 35, 35);
    [_topView addSubview:btn_Setting];
    [btn_Setting addTarget:self action:@selector(SettingAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

//左返回
- (void )btn_returnAction:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//
//右侧...
- (void)SettingAction:(UIButton *)sender
{
//    NSLog(@"点击进入...页面");
    
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
    [self presentViewController:alert animated:YES completion:^{
    }];
    
    UIAlertAction * action_1 = [UIAlertAction actionWithTitle:@"设置星标" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:action_1];
    
    UIAlertAction * action_2 = [UIAlertAction actionWithTitle:@"导出到手机" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:action_2];

    UIAlertAction * action_3 = [UIAlertAction actionWithTitle:@"更新通讯录" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:action_3];

    UIAlertAction * action_4 = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:action_4];
}

//显示tabbar
- (void )viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
}

#pragma mark - 布局
- (void )p_setupView
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    //
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 + 50, SCREEN_WIDTH, SCREEN_HEIGHT - 114) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.tableFooterView = [[UIView alloc] init];
    //
    self.tableView.sectionIndexColor = [UIColor blackColor];
    self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
    //
    [self p_TableViewHead];
    self.tableView.tableHeaderView = self.tableView_Head;
    
    //注册
    [self.tableView registerClass:[Mine_AddressbookTableViewCell class] forCellReuseIdentifier:@"cell_Person"];
}

#pragma mark - tableview代理
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    if([tableView isEqual:self.tableView])
    {
        return 11;
    }
    else if([tableView isEqual:self.tableView_Department])
    {
        return 7;
    }
    else
    {
        return 1;
    }
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if([tableView isEqual:self.tableView])
    {
        return 3;
    }
    else if([tableView isEqual:self.tableView_Department])
    {
        return 2;
    }
    else
    {
        return 11;
    }
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([tableView isEqual:self.tableView])
    {
        Mine_AddressbookTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell_Person" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.image_Head.image = [UIImage imageNamed:@"heads.jpg"];
        cell.name.text = @"员工姓名";
        cell.department.text = @"销售X部-销售经理";
        
        cell.btn_call.tag = indexPath.section * 300 + indexPath.row;
        [cell.btn_call addTarget:self action:@selector(btn_callAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        return cell;
    }
    else if([tableView isEqual:self.tableView_Department])
    {
        Select_DirectlyleaderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell_Department" forIndexPath:indexPath];
        cell.btn_Select.userInteractionEnabled = NO;
        
        //
        cell.image_Head.image = [UIImage imageNamed:@"Department.png"];
        cell.user_Name.text = @"XX销售部";
        cell.user_Department.text = [NSString stringWithFormat:@"%ld人",indexPath.row + 10];
        
        return cell;
    }
    else
    {
        Select_DirectlyleaderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell_Conversation" forIndexPath:indexPath];
        cell.btn_Select.userInteractionEnabled = NO;
        
        //
        cell.image_Head.image = [UIImage imageNamed:@"heads.jpg"];
        cell.user_Name.text = @"群名称---";
        cell.user_Department.text = [NSString stringWithFormat:@"%ld人",indexPath.row + 20];
        
        return cell;
    }
}

- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

//左边的
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if([tableView isEqual:self.tableView])
    {
        NSArray * arr_data = @[@"A",@"B",@"D",@"E",@"F",@"G",@"T",@"W",@"X",@"Y",@"Z"];
        return arr_data[section];
    }
    else if([tableView isEqual:self.tableView_Department])
    {
        NSArray * arr_data = @[@"我的部门",@"B",@"D",@"E",@"F",@"G",@"T"];
        return arr_data[section];
    }
    else
    {
        return @"最近的群组";
    }
}

//右边的
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    if([tableView isEqual:self.tableView])
    {
        NSArray * arr_data = @[@"A",@"B",@"D",@"E",@"F",@"G",@"T",@"W",@"X",@"Y",@"Z"];
        
        return arr_data;
    }
    else if([tableView isEqual:self.tableView_Department])
    {
        NSArray * arr_data = @[@"我",@"B",@"D",@"E",@"F",@"G",@"T"];
        
        return arr_data;
    }
    else
    {
        return nil;
    }
}

#pragma mark - 点击拨打电话
- (void )btn_callAction:(UIButton *)sender
{
//    CGFloat length_section = sender.tag / 300;
//    CGFloat length_row = sender.tag % 300;

    NSLog(@"点击拨打电话");
}

#pragma mark - 头部选择
- (void )p_headView
{
    self.view_Head = [[UIView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 50)];
    self.view_Head.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:self.view_Head];
    
    CGFloat length_X = SCREEN_WIDTH / 3;
    
    self.btn_1 = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.btn_1.frame = CGRectMake(0, 0, length_X, 50);
    [self.btn_1 setTitle:@"同事" forState:(UIControlStateNormal)];
    [self.btn_1 setTitleColor:[UIColor orangeColor] forState:(UIControlStateNormal)];
    self.btn_1.titleLabel.font = [UIFont systemFontOfSize:17.0f];
    self.btn_1.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.view_Head addSubview:self.btn_1];
    [self.btn_1 addTarget:self action:@selector(btn_1Action:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    self.btn_2 = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.btn_2.frame = CGRectMake(length_X, 0, length_X, 50);
    [self.btn_2 setTitle:@"部门" forState:(UIControlStateNormal)];
    [self.btn_2 setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    self.btn_2.titleLabel.font = [UIFont systemFontOfSize:17.0f];
    self.btn_2.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.view_Head addSubview:self.btn_2];
    [self.btn_2 addTarget:self action:@selector(btn_2Action:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    self.btn_3 = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.btn_3.frame = CGRectMake(length_X * 2, 0, length_X, 50);
    [self.btn_3 setTitle:@"服务号" forState:(UIControlStateNormal)];
    [self.btn_3 setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    self.btn_3.titleLabel.font = [UIFont systemFontOfSize:17.0f];
    self.btn_3.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.view_Head addSubview:self.btn_3];
    [self.btn_3 addTarget:self action:@selector(btn_3Action:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //
    self.view_Headline = [[UIView alloc] init];
    self.view_Headline.frame = CGRectMake(0, 48, length_X, 2);
    self.view_Headline.backgroundColor = [UIColor orangeColor];
    [self.view_Head addSubview:self.view_Headline];
}

#pragma mark - 头部视图点击方法
- (void )btn_1Action:(UIButton *)sender
{
    [self.btn_1 setTitleColor:[UIColor orangeColor] forState:(UIControlStateNormal)];
    [self.btn_2 setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    [self.btn_3 setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    
    self.tableView.hidden = NO;
    self.tableView_Department.hidden = YES;
    self.tableView_Conversation.hidden = YES;
    
    [UIView animateWithDuration:0.5f animations:^{
        
        self.view_Headline.frame = CGRectMake(0, 48, SCREEN_WIDTH / 3, 2);
    } completion:^(BOOL finished) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            //刷新tableView(记住,要更新放在主线程中)
            [self.tableView reloadData];
        });
    }];
}

- (void )btn_2Action:(UIButton *)sender
{
    [self.btn_2 setTitleColor:[UIColor orangeColor] forState:(UIControlStateNormal)];
    [self.btn_1 setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    [self.btn_3 setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    
    self.tableView_Department.hidden = NO;
    self.tableView.hidden = YES;
    self.tableView_Conversation.hidden = YES;
    
    [UIView animateWithDuration:0.5f animations:^{
        
        self.view_Headline.frame = CGRectMake(SCREEN_WIDTH / 3, 48, SCREEN_WIDTH / 3, 2);
    } completion:^(BOOL finished) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            //刷新tableView(记住,要更新放在主线程中)
            [self.tableView reloadData];
        });
    }];
}

- (void )btn_3Action:(UIButton *)sender
{
    [self.btn_3 setTitleColor:[UIColor orangeColor] forState:(UIControlStateNormal)];
    [self.btn_1 setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    [self.btn_2 setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    
    self.tableView.hidden = YES;
    self.tableView_Department.hidden = YES;
    self.tableView_Conversation.hidden = NO;
    
    [UIView animateWithDuration:0.5f animations:^{
        
        self.view_Headline.frame = CGRectMake(SCREEN_WIDTH / 3 * 2, 48, SCREEN_WIDTH / 3, 2);
    } completion:^(BOOL finished) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            //刷新tableView(记住,要更新放在主线程中)
            [self.tableView reloadData];
        });
    }];
}

#pragma mark - tableview内部头视图
- (void )p_TableViewHead
{//tableview内部头视图
    self.tableView_Head = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 60)];
    self.tableView_Head.backgroundColor = [UIColor whiteColor];
    
    UILabel * lbl_detail = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 300, 20)];
    lbl_detail.text = @"按组织架构查看";
    lbl_detail.font = [UIFont systemFontOfSize:17.0f];
    [self.tableView_Head addSubview:lbl_detail];
    
    UIImageView * icon_return = [[UIImageView alloc] init];
    icon_return.frame = CGRectMake(SCREEN_WIDTH - 15 - 20, 20, 20, 20);
    icon_return.image = [UIImage imageNamed:@"rigth-return.png"];
    icon_return.layer.masksToBounds = YES;
    [self.tableView_Head addSubview:icon_return];
    
    UIButton * btn_tableHead = [UIButton buttonWithType:(UIButtonTypeCustom)];
    btn_tableHead.frame = CGRectMake(0, 0, SCREEN_WIDTH, 60);
    [self.tableView_Head addSubview:btn_tableHead];
    [btn_tableHead addTarget:self action:@selector(btn_tableHeadAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

#pragma mark - 点击组织架构查看
- (void )btn_tableHeadAction:(UIButton *)sender
{
    NSLog(@"点击组织架构查看");
}

@end
